var wxCharts = require('../../../utils/wxcharts');
var app = getApp();
var pieChart = null;
var lineChart = null;
let bookName = '',
    curr_data = 0,
    windowWidth = 320;
Page({
    data: {
    },
    touchHandler: function (e) {
        console.log(pieChart.getCurrentDataIndex(e));
        lineChart.scrollStart(e);
    },     

    moveHandler: function (e) {
      lineChart.scroll(e);
    },
    touchEndHandler: function (e) {
        lineChart.scrollEnd(e);
        lineChart.showToolTip(e, {
            format: function (item, category) {
                return category + ' ' + item.name + ':' + item.data 
            }
        });        
    },

    // 同步日期
        createSimulationData: function () {
          let data = [],
            categories = [],
            arr = wx.getStorageSync('lineData');
        console.log(arr)
        arr.forEach(item =>{
            categories.push(item.time)
            data.push(item.today_cnt)
        })

          return {
              categories: categories,
              data: data
          }
      },

    onLoad: function (e) {
        var simulationData = this.createSimulationData();
        console.log('simu',simulationData)
        lineChart = new wxCharts({
            canvasId: 'lineCanvas',
            type: 'line',
            categories: simulationData.categories,
            animation: false,
            series: [{
                name: '今日已学',
                data: simulationData.data,
                format: function (val, name) {
                    return val.toFixed(0) + '个';
                }
            }],
            xAxis: {
                disableGrid: false
            },
            yAxis: { 
                title: '单词数',
                format: function (val) {
                    return val.toFixed(0);
                },
                min: 0,
                max:150
            },
            width: windowWidth,
            height: 200,
            dataLabel: true,
            dataPointShape: true,
            enableScroll: true,
            extra: {
                lineStyle: 'curve'
            }
        });
    },

    async onShow() {
        // 根据当前书信息
        let storbook = await wx.getStorageSync('currBook')
        console.log('书：', storbook)
        if (storbook.type == 'cet4' && wx.getStorageSync('cet4Progress')) {
            curr_data = wx.getStorageSync('curr_plan')
            console.log(typeof curr_data.schedule)
            this.setData({
                bookName: storbook.name,
            })
        }else if(storbook.type == 'cet6' && wx.getStorageSync('cet6Progress')) {
            curr_data = wx.getStorageSync('curr_plan')
            console.log(curr_data.schedule)
            this.setData({
                bookName: storbook.name,
            })
        }else if(storbook.type == 'kaoyan' && wx.getStorageSync('kaoyanProgress')) {
            curr_data = wx.getStorageSync('curr_plan')
            console.log(curr_data.schedule)
            this.setData({
                bookName: storbook.name,
            })
        }

        // pie显示
        pieChart = new wxCharts({
            animation: true,
            canvasId: 'pieCanvas',
            type: 'pie',
            series: [{
                name: '已经完成',
                data: Number(curr_data.schedule),
            },{
                name: '未完成',
                data: 100 - Number(curr_data.schedule),
            }],
            width: windowWidth,
            height: 280,
            dataLabel: true,
        });
    }
});